#include "stdafx.h"
#include "PWGameStartup.h"

PWGameStartup::PWGameStartup()
{

}

PWGameStartup::~PWGameStartup()
{

}

IGameRef PWGameStartup::Init()
{
	IGameRef pGame;

	return pGame;
}

void PWGameStartup::Shutdown()
{
	this->~PWGameStartup();
}

int PWGameStartup::Update( bool haveFocus, unsigned int updateFlags )
{
	return 1;
}

int PWGameStartup::Run( const char * autoStartLevelName )
{
	//windows message pump
	for(;;)
	{
		MSG msg;
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			if (msg.message != WM_QUIT)
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				break;
			}
		}
		else
		{
			if (! Update(true, 0))
			{
				// need to clean the message loop (WM_QUIT might cause problems in the case of a restart)
				// another message loop might have WM_QUIT already so we cannot rely only on this 
				while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
				break;
			}
		}
	}
	return 0;
}

LRESULT	CALLBACK PWGameStartup::WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	return 0;
}

bool PWGameStartup::InitWindow( /*SSystemInitParams& startupParams*/ )
{
#ifdef WIN32
	WNDCLASS wc;

	memset(&wc, 0, sizeof(WNDCLASS));

	wc.style         = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wc.lpfnWndProc   = (WNDPROC)PWGameStartup::WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = GetModuleHandle(0);
	// FIXME: Very bad way of getting the Icon and Cursor from the Launcher project
	wc.hIcon         = NULL;//LoadIcon((HINSTANCE)startupParams.hInstance, MAKEINTRESOURCE(101));
	wc.hCursor       = NULL;//LoadCursor((HINSTANCE)startupParams.hInstance, MAKEINTRESOURCE(DEFAULT_CURSOR_RESOURCE_ID));
	wc.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = GAME_WINDOW_CLASSNAME;

	if (!RegisterClass(&wc))
	{
		return false;
	}

// 	if (startupParams.pSystem == NULL || (!startupParams.pSystem->IsEditor() && !startupParams.pSystem->IsDedicated()))
// 		::ShowCursor(FALSE);

#endif WIN32

	return true;
}

void PWGameStartup::ShutdownWindow()
{
#ifdef WIN32
	if (true/*m_initWindow*/)
	{
		UnregisterClass(GAME_WINDOW_CLASSNAME, GetModuleHandle(0));
	}
#endif
}

bool PWGameStartup::InitFramework( /*SSystemInitParams& startupParams*/ )
{
	return true;
}

void PWGameStartup::ShutdownFramework()
{

}
